package com.dzt.cloudreader;

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.dzt.cloudreader.base.FrameActivity;
import com.dzt.cloudreader.constant.ConstantsImageUrl;
import com.dzt.cloudreader.fragment.BookFragment;
import com.dzt.cloudreader.fragment.GankFragment;
import com.dzt.cloudreader.fragment.MyFragmentPagerAdapter;
import com.dzt.cloudreader.fragment.OneFragment;
import com.dzt.cloudreader.interfaces.PerfectClickListener;
import com.dzt.cloudreader.rxbus.RxBus;
import com.dzt.cloudreader.rxbus.RxBusBaseMessage;
import com.dzt.cloudreader.rxbus.RxCodeConstants;
import com.dzt.cloudreader.utils.ImgLoadUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

/**
 * 程序主界面，负责加载各Fragment和切换Fragment
 */
public class MainActivity extends FrameActivity
		implements ViewPager.OnPageChangeListener {

	@BindView(R.id.ll_title_menu)
	FrameLayout llTitleMenu;
	@BindView(R.id.toolbar)
	Toolbar toolbar;
	@BindView(R.id.nav_view)
	NavigationView navView;
	@BindView(R.id.dl_content)
	DrawerLayout drawerLayout;
	@BindView(R.id.vp_content)
	ViewPager vpContent;
	@BindView(R.id.iv_title_gank)
	ImageView llTitleGank;
	@BindView(R.id.iv_title_one)
	ImageView llTitleOne;
	@BindView(R.id.iv_title_dou)
	ImageView llTitleDou;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_main;
	}

	@Override
	protected void initParams() {
		initRxBus();
		initContentFragment();
		initDrawerLayout();
	}

	/**
	 * inflateHeaderView 进来的布局要宽一些
	 */
	private void initDrawerLayout() {
		navView.inflateHeaderView(R.layout.nav_header_main);
		View headerView = navView.getHeaderView(0);

		SwitchCompat dayNightSwitch = headerView.findViewById(R.id.day_night_switch);
		dayNightSwitch.setChecked(preferenceKit.getNightMode());

		ImageView ivAvatar = headerView.findViewById(R.id.iv_avatar);

		ImgLoadUtil.displayCircle(ivAvatar, ConstantsImageUrl.IC_AVATAR);

		headerView.findViewById(R.id.iv_avatar).setOnClickListener(listener);
		headerView.findViewById(R.id.ll_nav_exit).setOnClickListener(listener);
		headerView.findViewById(R.id.ll_nav_homepage).setOnClickListener(listener);
		headerView.findViewById(R.id.ll_nav_scan_download).setOnClickListener(listener);
		headerView.findViewById(R.id.ll_nav_deedback).setOnClickListener(listener);
		headerView.findViewById(R.id.ll_nav_about).setOnClickListener(listener);
		headerView.findViewById(R.id.ll_nav_login).setOnClickListener(listener);
	}

	private void initContentFragment() {
		ArrayList<Fragment> mFragmentList = new ArrayList<>();
		mFragmentList.add(new GankFragment());
		mFragmentList.add(new OneFragment());
		mFragmentList.add(new BookFragment());
		// 注意使用的是：getSupportFragmentManager
		MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList);
		vpContent.setAdapter(adapter);
		// 设置ViewPager最大缓存的页面个数(cpu消耗少)
		vpContent.setOffscreenPageLimit(2);
		vpContent.addOnPageChangeListener(this);
		llTitleGank.setSelected(true);
		vpContent.setCurrentItem(0);

		setSupportActionBar(toolbar);
		ActionBar actionBar = getSupportActionBar();
		if (actionBar != null) {
			//去除默认Title显示
			actionBar.setDisplayShowTitleEnabled(false);
		}
	}

	/**
	 * 每日推荐点击"新电影热映榜"跳转
	 */
	private void initRxBus() {
		Subscription subscription = RxBus.getDefault().toObservable(RxCodeConstants.JUMP_TYPE_TO_ONE,
				RxBusBaseMessage.class)
				.subscribe(new Action1<RxBusBaseMessage>() {
					@Override
					public void call(RxBusBaseMessage integer) {
						vpContent.setCurrentItem(1);
					}
				});
		addSubscription(subscription);
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
			case 0:
				llTitleGank.setSelected(true);
				llTitleOne.setSelected(false);
				llTitleDou.setSelected(false);
				break;
			case 1:
				llTitleOne.setSelected(true);
				llTitleGank.setSelected(false);
				llTitleDou.setSelected(false);
				break;
			case 2:
				llTitleDou.setSelected(true);
				llTitleOne.setSelected(false);
				llTitleGank.setSelected(false);
				break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

	private PerfectClickListener listener = new PerfectClickListener() {
		@Override
		protected void onNoDoubleClick(final View v) {
			drawerLayout.closeDrawer(GravityCompat.START);
			drawerLayout.postDelayed(new Runnable() {
				@Override
				public void run() {
					switch (v.getId()) {
						case R.id.ll_nav_homepage:// 主页
							//NavHomePageActivity.startHome(MainActivity.this);
							break;
						case R.id.ll_nav_scan_download://扫码下载
							//NavDownloadActivity.start(MainActivity.this);
							break;
						case R.id.ll_nav_deedback:// 问题反馈
							//NavDeedBackActivity.start(MainActivity.this);
							break;
						case R.id.ll_nav_about:// 关于云阅
							//NavAboutActivity.start(MainActivity.this);
							break;
						case R.id.ll_nav_login:// 登录GitHub账号
							//WebViewActivity.loadUrl(v.getContext(), "https://github.com/login", "登录GitHub账号");
							break;
						case R.id.iv_avatar:
							//WebViewActivity.loadUrl(v.getContext(),CommonUtils.getString(R.string.string_url_cloudreader),"CloudReader");
							break;
						case R.id.ll_nav_exit:
							onBackPressed();
							break;

					}
				}
			}, 260);
		}
	};

	@OnClick({R.id.ll_title_menu, R.id.iv_title_gank,
			R.id.iv_title_one, R.id.iv_title_dou})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.ll_title_menu:
				drawerLayout.openDrawer(GravityCompat.START);
				break;
			case R.id.iv_title_gank:
				if (vpContent.getCurrentItem() != 0) {//不然cpu会有损耗
					llTitleGank.setSelected(true);
					llTitleOne.setSelected(false);
					llTitleDou.setSelected(false);
					vpContent.setCurrentItem(0);
				}
				break;
			case R.id.iv_title_one:
				if (vpContent.getCurrentItem() != 1) {
					llTitleOne.setSelected(true);
					llTitleGank.setSelected(false);
					llTitleDou.setSelected(false);
					vpContent.setCurrentItem(1);
				}
				break;
			case R.id.iv_title_dou:
				if (vpContent.getCurrentItem() != 2) {
					llTitleDou.setSelected(true);
					llTitleOne.setSelected(false);
					llTitleGank.setSelected(false);
					vpContent.setCurrentItem(2);
				}
				break;
		}
	}

	@Override
	public void onBackPressed() {
		if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
			drawerLayout.closeDrawer(GravityCompat.START);
		} else {
			super.onBackPressed();
		}
	}
}
